<template>
    <div class="container van-hairline--top">
        <!-- 暂无数据组件 -->
        <Nodata v-if="isNoData"></Nodata>
        <div v-else class="content-box">
            <van-list v-model="loading" :finished="finished" @load="onLoad" :offset="0" v-for="(item, index) in dataList" :key="index" >
                   <van-swipe-cell :right-width="65" :on-close="onClose" @click="acivt(item)">
                        <van-cell-group>
                            <van-cell  :value="$route.query.id==1?'编辑':'查看'" @click="edit(item)">
                                <template slot="title">
                                <p class="van-cell-text one-txt-cut">{{item.article_title}}</p>
                                <p class="van-cell-time">{{item.create_time}}</p>
                                </template>
                            </van-cell>
                         </van-cell-group>
                        <span slot="right">删除</span>
                    </van-swipe-cell>
            </van-list>
        </div>

    </div>
</template>

<script>
import Nodata from "../public/nodata/Nodata"; // 导入暂无数据组件
import { article_list, article_listMyCollect, article_listMy, article_delete } from "@/api/article";
import { clacTime } from "@/utils";
import TitleItem from "../../components/public/title/title";
export default {
  name: "MyPost",
  components: { Nodata, TitleItem },
  data() {
    return {
      isNoData: false,
      loading: false,
      finished: false,
      acivtId:'',
      listQuery: {
        current: 1,
        limit: 5,
        company_id: localStorage.getItem("companyId")
      },
      dataList: [],
      aaa:[],
      total: "",

    };
  },
  methods: {
    //编辑贴子

    edit(item) {
      if (this.$route.query.id == 1) {
        this.$router.push({
          path: "/CommunityTheme",
          query: {
            name: "edit",
            content: item
          }
        });
      } else {
        this.$router.push({
          path: "/CommunityDetail",
          query: {
            id: item.id,
            name: 'article'
          }
        });
      }
    },
    acivt(item){
      this.acivtId = item.id
    },
     onClose(clickPosition, instance) {
      switch (clickPosition) {
        case 'left':
        case 'cell':
        case 'outside':
          instance.close();
          break;
        case 'right':
         this.$dialog.confirm({
            message: '确定删除吗？'
          }).then(() => {
            this.dele()
            instance.close();
          });
          break;
      }
    },
    async dele(){
        let par = {
            id:this.acivtId
        }
         let {data}= this.$route.query.id == 1 ? await article_delete(par) : await article_listMyCollect(query);
         if(data.status ==100){
             this.$toast('删除成功')
             this.initList()

         }
    },
    //获取数据
    async initList() {
      let user = JSON.parse(localStorage.getItem("user"));
      this.listQuery.create_user = user.id;
      let query = Object.assign(this.listQuery);
      let res =
        this.$route.query.id == 1
          ? await article_listMy(query)
          : await article_listMyCollect(query);
      let result = res.data;
      if (result.status == 100) {
        this.dataList = result.data.rows.map(item => {
          return Object.assign({}, item, {
            create_time: clacTime(item.create_time)
          });
        });
        this.total = result.data.results;
        if (!this.total) {
          this.isNoData = true;
        }
      }
      this.loading = false;
      if (result.data.results <= this.dataList.length) {
        this.finished = true;
      }
    },
    onLoad() {
      setTimeout(() => {
        this.listQuery.limit += 2;
        this.initList();
      }, 1000);
    }
  },
  created() {
    this.initList();

  }
};
</script>

<style lang="scss" scoped>
@import "MyPost";

</style>
